package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import org.joda.time.DateTime;

import database.DatabaseExtension;
import database.MyAddressee;
import database.MyClient;
import database.MyDestination;
import database.MyIncoterm;
import database.MyShipment;
import database.NewTirDataBase;

import Other.Glob;
import enums.DialogType;
import enums.SearchType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;


public class Shipment extends JFrame {
	
	private static Shipment shipmentInstance = null;
	private JPanel contentPane;
	private JTextField tfCode;
	private JTextField tfClientCode;
	private JTextField tfClientName;
	private JTextField tfAddresseeName;
	private JTextField tfAddresseeCode;
	private JTextField tfInvoice;
	private JTextField tfRDate;
	private JTextField tfDDate;
	private JTextField tfCurrency;
	private JTextField tfKilos;
	private JTextField tfVolume;
	private JTextField tfIncotermName;
	private JTextField tfIncotermCode;
	private JTextField tfDestinationCode;
	private JTextField tfDestinationName;
	//variable that represent this window frame
	private JFrame thisFrame = this;
	//variable to enable and disable the screen
	boolean disable;
	private JTextField tfComment1;
	private JTextField tfComment2;

	/**
	 * Create the frame.
	 */
	private Shipment() {
		setResizable(false);
		setTitle("Shipment");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 854, 438);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel leftPanel = new JPanel();
		leftPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JPanel rightPanel = new JPanel();
		rightPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JPanel southPanel = new JPanel();
		southPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JButton btnRemove = new JButton("F6 Remove");
		btnRemove.getInputMap(btnRemove.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
		btnRemove.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 //remove item when F6 button pressed
				ScreenHandling.remove(tfCode, SearchType.SHIPMENT);
			    }
		});
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//remove item when button pressed
				ScreenHandling.remove(tfCode, SearchType.SHIPMENT);
			}
		});
		
		JButton btnEscExit = new JButton("Esc Exit");
		
		btnEscExit.getInputMap(btnEscExit.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
		btnEscExit.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				if (disable == false){
				//escape from the screen
					escape();
				}
			    }
		});
		
		btnEscExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//if the screen is disable it is possible to escape
				if (disable == false){
					escape();
				}
			}
		});
		
		JButton btnReset = new JButton("F7 Reset");
		//set up F7 for reset
		btnReset.getInputMap(btnReset.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
		btnReset.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				//reset field when button pressed
				 resetFields();
			    }
		});
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//reset field when button pressed
				resetFields();
			}
		});
		
		JButton btnSave = new JButton("F4 Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Save when button pressed
				Glob.type = SearchType.SHIPMENT;
				try {
					saveDialog();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
			}
		});
		
		
		
		JButton btnAddItems = new JButton("F8 Add Items");
	
		btnAddItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				checkShipmentBeforeAddItems();
			}
		});
		
		
		GroupLayout gl_southPanel = new GroupLayout(southPanel);
		gl_southPanel.setHorizontalGroup(
			gl_southPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_southPanel.createSequentialGroup()
					.addGap(18)
					.addComponent(btnRemove)
					.addGap(102)
					.addComponent(btnAddItems)
					.addPreferredGap(ComponentPlacement.RELATED, 154, Short.MAX_VALUE)
					.addComponent(btnEscExit)
					.addGap(29)
					.addComponent(btnReset)
					.addGap(34)
					.addComponent(btnSave)
					.addGap(18))
		);
		gl_southPanel.setVerticalGroup(
			gl_southPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_southPanel.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_southPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSave)
						.addComponent(btnRemove)
						.addComponent(btnReset)
						.addComponent(btnEscExit)
						.addComponent(btnAddItems))
					.addContainerGap(38, Short.MAX_VALUE))
		);
		southPanel.setLayout(gl_southPanel);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 414, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(rightPanel, GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
						.addComponent(southPanel, GroupLayout.DEFAULT_SIZE, 828, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(leftPanel, GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
						.addComponent(rightPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
					.addGap(18)
					.addComponent(southPanel, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
		);
		
		JLabel label_6 = new JLabel("D. Date");
		
		tfDDate = new JTextField();
		
		tfDDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfCurrency.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfDDate.getInputMap(tfRDate.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "");
		tfDDate.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				//DateTime date =  new org.joda.time.DateTime();
				
				//tfRDate.setText(date.toLocalDate().toString());
				 
				 String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
				
				 tfDDate.setText(timeStamp);
				 
				 //tfDDate.getText().replace("d", "");
				
				 
			    }
		});
		tfDDate.setColumns(10);
		
		JLabel lblCurrency = new JLabel("Currency");
		
		tfCurrency = new JTextField();
		
		tfCurrency.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfKilos.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfCurrency.setColumns(10);
		
		JLabel lblKilos = new JLabel("Kilos");
		
		tfKilos = new JTextField();
		tfKilos.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfVolume.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		tfKilos.setColumns(10);
		
		JLabel lblVolume = new JLabel("Volume");
		
		tfVolume = new JTextField();
		tfVolume.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfIncotermCode.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		tfVolume.setColumns(10);
		
		JLabel lblIncoterm = new JLabel("Incoterm ");
		
		tfIncotermName = new JTextField();
		tfIncotermName.setEditable(false);
		tfIncotermName.setColumns(10);
		
		tfIncotermCode = new JTextField();
		
		tfIncotermCode.getInputMap(tfIncotermCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfIncotermCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				Glob.type= SearchType.SHIPMENTINCOTERM;
				 
				search();
				
			    }
		});
		
		tfIncotermCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
					ScreenHandling.enterData(tfIncotermCode, tfIncotermName, tfComment1, SearchType.INCOTERM);
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		
		
		
		tfIncotermCode.setColumns(10);
		
		JButton sbIncoterm = new JButton("F5 Search");
		sbIncoterm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.SHIPMENTINCOTERM;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		JLabel lblComments = new JLabel("Comments");
		
		tfComment1 = new JTextField();
		tfComment1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfComment2.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		tfComment1.setColumns(10);
		
		tfComment2 = new JTextField();
		tfComment2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfCode.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		tfComment2.setColumns(10);
		GroupLayout gl_rightPanel = new GroupLayout(rightPanel);
		gl_rightPanel.setHorizontalGroup(
			gl_rightPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_rightPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblIncoterm, GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblCurrency, GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
							.addComponent(lblKilos, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblVolume, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addComponent(lblComments)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(tfKilos, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfCurrency, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfVolume, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfDDate, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE))
							.addGap(192))
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addComponent(tfIncotermCode, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(tfIncotermName, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
									.addGap(18)
									.addComponent(sbIncoterm, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE))
								.addComponent(tfComment1, GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
								.addComponent(tfComment2, GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))
							.addContainerGap())))
		);
		gl_rightPanel.setVerticalGroup(
			gl_rightPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_rightPanel.createSequentialGroup()
					.addGap(17)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_6)
						.addComponent(tfDDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCurrency)
						.addComponent(tfCurrency, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKilos)
						.addComponent(tfKilos, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(tfVolume, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblVolume))
					.addGap(18)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfIncotermCode, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblIncoterm)
						.addComponent(tfIncotermName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(sbIncoterm))
					.addGap(18)
					.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addComponent(tfComment1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
							.addComponent(tfComment2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(lblComments))
					.addContainerGap())
		);
		rightPanel.setLayout(gl_rightPanel);
		
		JLabel label = new JLabel("Code");
		
		tfCode = new JTextField();
		
		tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "Save");
		tfCode.getActionMap().put("Save", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				//Save when button pressed
				 Glob.type = SearchType.SHIPMENT;
				 try {
						saveDialog();
					} catch (Exception b) {
						// TODO Auto-generated catch block
						b.printStackTrace();
					}
			 }
		});
		//set up F5 for saving only when the text is focused
		tfCode.getInputMap(tfCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 shipmentSearchOption();
			    }
		});
		tfCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a number is entered with ENTER
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					
					enterData();
					
					//tfName.requestFocus();
				}
				
			}
		});
		
		tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), "AddItems");
		tfCode.getActionMap().put("AddItems", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 checkShipmentBeforeAddItems();
			 }
		});
		
		tfCode.setColumns(10);
		
		JButton sbCode = new JButton("F5 Search");
		sbCode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				 //when textcode focused and f5 pressed search dialog comes up
						 shipmentSearchOption();
			}
		});
		
		JLabel lblClient = new JLabel("Client");
		
		tfClientCode = new JTextField();
		
		
		
		tfClientCode.getInputMap(tfClientCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfClientCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.SHIPMENTCLIENT;
				 
				search();
				
			    }
		});
		
		tfClientCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
					ScreenHandling.enterData(tfClientCode, tfClientName, tfAddresseeCode, SearchType.CLIENT);
					System.out.println("button is being pressed");
					
				}
			}
		});
		tfClientCode.setColumns(10);
		
		tfClientName = new JTextField();
		tfClientName.setEditable(false);
		tfClientName.setColumns(10);
		
		JButton sbClient = new JButton("F5 Search");
		sbClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.SHIPMENTCLIENT;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		JButton sbAddressee = new JButton("F5 Search");
		sbAddressee.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.SHIPMENTADDRESSEE;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		tfAddresseeName = new JTextField();
		tfAddresseeName.setEditable(false);
		tfAddresseeName.setColumns(10);
		
		tfAddresseeCode = new JTextField();
		
		tfAddresseeCode.getInputMap(tfAddresseeCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfAddresseeCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.SHIPMENTADDRESSEE;
				 
				search();
				
			    }
		});
		
		tfAddresseeCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
					ScreenHandling.enterData(tfAddresseeCode, tfAddresseeName, tfInvoice, SearchType.ADDRESSEE);
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfAddresseeCode.setColumns(10);
		
		JLabel lblAddressee = new JLabel("Addressee");
		
		JLabel lblInvoiceNo = new JLabel("Invoice No");
		
		tfInvoice = new JTextField();
		
		tfInvoice.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfRDate.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfInvoice.setColumns(10);
		
		JLabel lblRDate = new JLabel("R. Date");
		
		tfRDate = new JTextField();
		
		tfRDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfDestinationCode.requestFocus();
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfRDate.getInputMap(tfRDate.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "");
		tfRDate.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				//DateTime date =  new org.joda.time.DateTime();
				
				//tfRDate.setText(date.toLocalDate().toString());
				 
				 String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
				 tfRDate.setText(timeStamp);
				 
			    }
		});
		
		tfRDate.setColumns(10);
		
		JLabel Destination = new JLabel("Destination");
		
		tfDestinationCode = new JTextField();
		tfDestinationCode.getInputMap(tfDestinationCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfDestinationCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.SHIPMENTDESTINATION;
				 
				search();
				
			    }
		});
		
		tfDestinationCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
					ScreenHandling.enterData(tfDestinationCode, tfDestinationName, tfDDate, SearchType.DESTINATION);
					System.out.println("button is being pressed");
					
				}
			}
		});
		
		tfDestinationCode.setColumns(10);
		
		JButton sbDestination = new JButton("F5 Search");
		sbDestination.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.SHIPMENTDESTINATION;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		tfDestinationName = new JTextField();
		tfDestinationName.setEditable(false);
		tfDestinationName.setColumns(10);
		GroupLayout gl_leftPanel = new GroupLayout(leftPanel);
		gl_leftPanel.setHorizontalGroup(
			gl_leftPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_leftPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblRDate, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblInvoiceNo, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblAddressee, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblClient, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
						.addComponent(Destination, GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addComponent(tfDestinationCode, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(tfDestinationName, 111, 111, 111)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(sbDestination, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(tfRDate, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
							.addComponent(tfInvoice, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
							.addGroup(gl_leftPanel.createSequentialGroup()
								.addGroup(gl_leftPanel.createParallelGroup(Alignment.TRAILING, false)
									.addGroup(gl_leftPanel.createSequentialGroup()
										.addComponent(tfAddresseeCode, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(tfAddresseeName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
									.addGroup(gl_leftPanel.createSequentialGroup()
										.addComponent(tfClientCode, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(tfClientName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
									.addComponent(tfCode, Alignment.LEADING))
								.addGap(17)
								.addGroup(gl_leftPanel.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(sbCode, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(sbAddressee, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(sbClient, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)))))
					.addContainerGap())
		);
		gl_leftPanel.setVerticalGroup(
			gl_leftPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_leftPanel.createSequentialGroup()
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(15)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label)))
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addContainerGap()
							.addComponent(sbCode)))
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(20)
							.addComponent(sbClient))
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(24)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfClientCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfClientName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblClient))))
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(21)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfAddresseeCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfAddresseeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblAddressee)))
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(18)
							.addComponent(sbAddressee)))
					.addGap(18)
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(tfInvoice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblInvoiceNo))
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(38)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfRDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblRDate))))
					.addGap(18)
					.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(Destination)
						.addComponent(sbDestination)
						.addComponent(tfDestinationName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(tfDestinationCode, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(59, Short.MAX_VALUE))
		);
		leftPanel.setLayout(gl_leftPanel);
		contentPane.setLayout(gl_contentPane);
		
		//request focus when object is called
		tfCode.requestFocus(true);
	}
	
	//singleton
			public static Shipment getInstance(){
				if(shipmentInstance == null){
					shipmentInstance = new Shipment();
				}
				return shipmentInstance;
				}
	
			public void init(MyShipment shipment){
				
				
				
				tfCode.setText(shipment.getShipmentID());	
if(String.valueOf(shipment.getClientCodeID()).equals("null")){
					
					tfClientCode.setText("");
					
				}else{
				tfClientCode.setText(shipment.getClientCodeID());
				}
				
				
				tfClientName.setText(shipment.getClientName());
if(String.valueOf(shipment.getAddresseeCodeID()).equals("null")){
					
	tfAddresseeCode.setText("");
					
				}else{
					tfAddresseeCode.setText(shipment.getAddresseeCodeID());
				}
				
				tfAddresseeName.setText(shipment.getAddresseeName());
				tfInvoice.setText(shipment.getInvoice());
				if(String.valueOf(shipment.getrDate()).equals("null")){
					
					tfRDate.setText("");
					
				}else{
					tfRDate.setText(shipment.getrDate());
					
				}
			
if(String.valueOf(shipment.getDestinationCodeID()).equals("null")){
					
	tfDestinationCode.setText("");
					
				}else{
					tfDestinationCode.setText(shipment.getDestinationCodeID());
				}
				tfDestinationName.setText(shipment.getDestinationName());
if(String.valueOf(shipment.getdDate()).equals("null")){
					
	tfDDate.setText("");
					
				}else{
					tfDDate.setText(shipment.getdDate());
					
				}
				
				tfCurrency.setText(shipment.getCurrency());
				tfKilos.setText(shipment.getKilos());
				tfVolume.setText(shipment.getVolume());
				if(String.valueOf(shipment.getIncotermCodeID()).equals("null")){
					
					tfIncotermCode.setText("");
					
				}else{
					tfIncotermCode.setText(shipment.getIncotermCodeID());
				}
				
				
				tfIncotermName.setText(shipment.getIncotermName());
				
				tfComment1.setText(shipment.getComment1());
				tfComment2.setText(shipment.getComment2());
				
			}
			
			public void initClient(MyClient client){
				
				tfClientCode.setText(client.getClientID());
				tfClientName.setText(client.getName());
				
			}
			
			public void initAddressee(MyAddressee addressee){
				
				tfAddresseeCode.setText(addressee.getAddresseeID());
				tfAddresseeName.setText(addressee.getName());
				
			}
			


public void initDestination(MyDestination destination){
				
				tfDestinationCode.setText(destination.getDestinationID());
				tfDestinationName.setText(destination.getName());
				
			}

public void initIncoterm(MyIncoterm incoterm){
	
	tfIncotermCode.setText(incoterm.getIncotermID());
	tfIncotermName.setText(incoterm.getIncotermName());
	
}

//method needed to set focus from other screens
		public void setTextCodeFocus(){
			tfCode.requestFocus();
		}
		
		//reset fields
				public void resetFields(){
					tfCode.setText("");
					tfClientCode.setText("");
					tfClientName.setText("");
					tfAddresseeCode.setText("");
					tfAddresseeName.setText("");
					tfInvoice.setText("");
					tfRDate.setText("");
					tfDestinationCode.setText("");
					tfDestinationName.setText("");
					tfDDate.setText("");
					tfCurrency.setText("");
					tfKilos.setText("");
					tfVolume.setText("");
					tfIncotermCode.setText("");
					tfIncotermName.setText("");
					tfComment1.setText("");
					tfComment2.setText("");
					tfCode.requestFocus();
				}
				
				public String getFieldCode() {
				    return tfCode.getText();
				 }
				
				public String getClientCode() {
				    return tfClientCode.getText();
				 }
				
				
				
				//method for saving the object
				public void saveDialog() throws Exception{
					Glob.setDialog(DialogType.OVERWRITE);
					try{
						if(tfClientCode.getText().isEmpty()) {
							JOptionPane.showMessageDialog(null, "Client code may not be empty");
							tfClientCode.requestFocus();
							return;
						}
						if(tfAddresseeCode.getText().isEmpty()) {
							JOptionPane.showMessageDialog(null, "Addressee code may not be empty");
							tfClientCode.requestFocus();
							return;
						}
					try{
					if(tfCode.getText().trim().length()>1) {
					if(Integer.parseInt(tfCode.getText())<=0){
						JOptionPane.showMessageDialog(null, "Code has to be a valid number");
						tfCode.setText("");
						tfCode.requestFocus();
						return;
						
					}
					}
					}catch(NumberFormatException n){
						
						JOptionPane.showMessageDialog(null, "Code number is to big or has an incorrect format");
						tfCode.setText("");
						tfCode.requestFocus();
						return;
						
					}
					String rDate = tfRDate.getText();
					String dDate = tfDDate.getText();
					String formatString = "yyyy-MM-dd";
					
					System.out.print("This is the rdate lenth " + rDate.length());
					System.out.print("This is the ddate lenth " + dDate.length());
					System.out.print("This is the format lenth " + formatString.length());
					
					
					/**
					if(!rDate.isEmpty() && rDate.length() != formatString.length()){
						
						JOptionPane.showMessageDialog(null, "R.Date must be a proper date, mi mierda");
						tfRDate.requestFocus();
						return;
					}
					*/			
						//if (dDate.split("-").length!=3)
							if(!rDate.isEmpty()){
							try {
								String[] s1 = rDate.split("-");
								DateTime dt = new DateTime(Integer.parseInt(s1[0]),Integer.parseInt(s1[1]),Integer.valueOf(s1[2]),0,0);
							}catch(Exception e) {
								// Error message
								JOptionPane.showMessageDialog(null, "R.Date must be a proper date, john's mierda");
								tfRDate.requestFocus();
								return;
							
							}
					}
							
							/**
					if(!dDate.isEmpty() && dDate.length() != formatString.length()){
						
						JOptionPane.showMessageDialog(null, "D.Date must be a proper date");
						tfDDate.requestFocus();
						return;
						
					}
					*/if(!dDate.isEmpty()){
							try {
								String[] s1 = dDate.split("-");
								DateTime dt = new DateTime(Integer.valueOf(s1[0]),Integer.valueOf(s1[1]),Integer.valueOf(s1[2]),0,0);
							}catch(Exception e) {
								// Error message
								JOptionPane.showMessageDialog(null, "D.Date must be a proper date, john's mierda");
								tfDDate.requestFocus();
								return;
							
							}
					}
					try{	
						if(tfKilos.getText().trim().length()>1) {
						if(Float.valueOf(tfKilos.getText())<=-1){
							JOptionPane.showMessageDialog(null, "Kilos has to be a positive number with two decimals the most");
							tfKilos.setText("");
							tfKilos.requestFocus();
							return;
						}
						}
						}catch (NumberFormatException type){
							
							type.printStackTrace();
							JOptionPane.showMessageDialog(null, "Kilos has to be a positive number with two decimals the most");
							tfKilos.setText(null);
							tfKilos.requestFocus();
							return;
						}
					
					try{	
						if(tfVolume.getText().trim().length()>1) {
						if(Float.valueOf(tfVolume.getText())<=-1){
							JOptionPane.showMessageDialog(null, "Volume has to be a positive number with two decimals the most");
							tfVolume.setText("");
							tfVolume.requestFocus();
							return;
						}
						}
						}catch (NumberFormatException type){
							
							type.printStackTrace();
							JOptionPane.showMessageDialog(null, "Volume has to be a positive number with two decimals the most");
							tfVolume.setText(null);
							tfVolume.requestFocus();
							return;
						}
									
					
					ScreenHandling.checkIdOnTable(tfClientCode, SearchType.CLIENT);
					
				
					ScreenHandling.checkIdOnTable(tfAddresseeCode, SearchType.ADDRESSEE );
					
				
					ScreenHandling.checkIdOnTable(tfDestinationCode, SearchType.DESTINATION);
					
					ScreenHandling.checkIdOnTable(tfIncotermCode, SearchType.INCOTERM);
					
					
						
					ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from shipment");
					//System.out.println(ids);
					boolean Ok = false;
					if(tfCode.getText().isEmpty()) {
						//Do nothing
						}else{
							
					for (int i: ids) {
						if (i == Integer.valueOf(tfCode.getText()))
								Ok=true;
					}		
						if (Ok) {
						//JOptionPane.showMessageDialog(null, "This ID is not on the Tax table");
						Dialog d = Dialog.getInstance();
						d.setVisible(true);
						Shipment.getInstance().disableShipment();
						//tfCode.requestFocus();
						return;
						
					}
						}
						if(Ok==false){
						save();			
						}
						
					}catch (NumberFormatException e){
						
						System.out.println("This is the general expetion inside saveDialog");
						JOptionPane.showMessageDialog(null, "Code must be a positive number");
						tfCode.setText("");
						tfCode.requestFocus();
						e.printStackTrace();
						return;
					 
					}
						
				}
			
				
						
						//method that saves the object in the database
						public void save(){
							
						  boolean insert;
						  Glob.setType(SearchType.SHIPMENT);
							
							String[] data = new String[14];	
							
							try{
							data[0] = tfCode.getText();	
							}catch(Exception code){
								
								code.printStackTrace();
								
							}
							System.out.println("This is the id got from the tfcode inside save" + data[0]);
							if(data[0].isEmpty()){
							data[0] = null;
							}
							try{
								if(tfClientCode.getText().isEmpty()){
									
									data[1] = null;
									
								}else{
									
									data[1] = tfClientCode.getText();
								}

							}catch(Exception client){
								client.printStackTrace();
								JOptionPane.showMessageDialog(null, "Client must be a proper integer number");
								return;
							}
						
							try{
								if(tfAddresseeCode.getText().isEmpty()){
									
									data[2] = null;
									
								}else{
									//System.out.println("The ELSE has happeneded");
									data[2] = tfAddresseeCode.getText();
								}

							}catch(Exception addressee){
								addressee.printStackTrace();
								JOptionPane.showMessageDialog(null, "Addressee must be a proper integer number");
								return;
							}
						
						data[3] = tfInvoice.getText();
						
						try{
							
						
							if(tfRDate.getText().isEmpty()){
								System.out.println("The Null has happeneded inside rdate");
								data[4] = null;
								
							}else{
								System.out.println("The ELSE has happeneded inside rdate");
								data[4] = tfRDate.getText();
							}
							
						

						}catch(Exception rdate){
							rdate.printStackTrace();
							JOptionPane.showMessageDialog(null, "R.Date must be a proper date");
							return;
						}
						
						try{
							if(tfDestinationCode.getText().isEmpty()){
								
								data[5] = null;
								
							}else{
								//System.out.println("The ELSE has happeneded");
								data[5] = tfDestinationCode.getText();
							}

						}catch(Exception destination){
							destination.printStackTrace();
							JOptionPane.showMessageDialog(null, "Destination must be a proper integer number");
							return;
						}
						
						try{
						
							if(tfDDate.getText().isEmpty()){
								
								data[6] = null;
								
							}else{
								//System.out.println("The ELSE has happeneded");
								data[6] = tfDDate.getText();
							}
							

						}catch(Exception ddate){
							ddate.printStackTrace();
							JOptionPane.showMessageDialog(null, "R.Date must be a proper date");
							return;
						}
						
						data[7] = tfCurrency.getText();
						
						try{
							if(tfKilos.getText().isEmpty()){
								
								data[8] = "0";
								
							}else{
								//System.out.println("The ELSE has happeneded");
								data[8] = tfKilos.getText();
							}

						}catch(Exception kilos){
							kilos.printStackTrace();
							JOptionPane.showMessageDialog(null, "Kilos must be a proper float number");
							return;
						}
						
						try{
							if(tfVolume.getText().isEmpty()){
								
								data[9] = "0";
								
							}else{
								//System.out.println("The ELSE has happeneded");
								data[9] = tfVolume.getText();
							}

						}catch(Exception volume){
							volume.printStackTrace();
							JOptionPane.showMessageDialog(null, "Volume must be a proper float number");
							return;
						}
						
						try{
							if(tfIncotermCode.getText().isEmpty()){
								
								data[10] = null;
								
							}else{
								
								data[10] = tfIncotermCode.getText();
							}

						}catch(Exception incoterm){
							incoterm.printStackTrace();
							JOptionPane.showMessageDialog(null, "Incoterm must be a proper integer number");
						}

						data[11] = tfComment1.getText();
						
						data[12] = tfComment2.getText();
						
						data[13] = null;
						
						
						//System.out.println("This is the data String" + data[0]+data[1]+data[2]+data[3]+data[4]+data[5]+data[6]+data[7]+data[8]+data[9]+data[10]+data[11]+data[12]);
						
						//System.out.println("this is the string of data from Addressee in the method save" + data[0] + data[1] + data[2]);
							MyShipment shipment = new MyShipment(data);
							//System.out.println("this is the data inside de object in the method save" + Tax.getTaxID() + Tax.getTaxName());
							try{
								insert = DatabaseExtension.saveShipment(shipment);
							}catch (Exception saveShipment){
								
								saveShipment.printStackTrace();
								System.out.println("This is the exception released in saving shipment");
								JOptionPane.showMessageDialog(null, "Enter a correct format on the date fields 'yyyy-mm-dd', please");
								tfRDate.requestFocus();
								return;
								
								
							}
								
								setTextCodeFocus();
								
								if(insert){
								tfCode.setText(String.valueOf(DatabaseExtension.getLastColumnId("Select * from shipment")));	
								}
								
								

						}
						
						
						
						public void search(){
							
							SearchWindow s = SearchWindow.getInstance();
							s.setTitle();
							s.setVisible(true);
							disableShipment();
						}
						
						public void shipmentSearchOption(){
							Glob.setType(SearchType.SHIPMENT);
							ShipmentSearchOption ship = ShipmentSearchOption.getInstance();
							ship.setVisible(true);
							disableShipment();
						}
						
						
						
						
						//method to disable the screen
						public void disableShipment(){
							Shipment.getInstance().disable();
							disable = true;
						}
						//method to enable the screen
						public void enableShipment(){
							Shipment.getInstance().enable();
							disable = false;
							if (Glob.type ==  SearchType.SHIPMENT){
								tfCode.requestFocus();
							}else if (Glob.type ==  SearchType.SHIPMENTADDRESSEE){
								tfAddresseeCode.requestFocus();
							}else if (Glob.type ==  SearchType.SHIPMENTCLIENT){
								tfClientCode.requestFocus();
							}else if (Glob.type ==  SearchType.SHIPMENTDESTINATION){
								tfDestinationCode.requestFocus();
							}else if (Glob.type ==  SearchType.SHIPMENTINCOTERM){
								tfIncotermCode.requestFocus();
							}
							
							Shipment.getInstance().toFront();
						}
						//method to get the disable boolean
						public boolean getDisable(){
							return disable;
						}
						
						
						//method that populates the screen fields with the object
						public void enterData(){
							try{
								//set up global variable for the Shipment screen
								Glob.setType(SearchType.SHIPMENT);
								//if textfield is 0 or less than 0
								try{
								if(tfCode.getText().trim().length()<1){
									tfClientCode.requestFocus();
									
								}
								else if(Integer.parseInt(tfCode.getText()) <= 0){
									JOptionPane.showMessageDialog(null, "Code must be a positive number");
									tfCode.setText("");
									tfCode.requestFocus();
									return;
								}
								}catch (NumberFormatException number){
									
									JOptionPane.showMessageDialog(null, "Code must be a positive number");
									tfCode.setText("");
									tfCode.requestFocus();
									return;
									
								}
							
							NewTirDataBase c = (MyShipment) DatabaseExtension.getTableID("SELECT * FROM shipment WHERE shipmentID = '" + tfCode.getText() +"'")[0];
							
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getClientCodeID()).equals("null")){
									
									tfClientCode.setText("");
									
								}else{
									tfClientCode.setText(((MyShipment) c).getClientCodeID());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the client code");
									e.printStackTrace();
								}
							tfClientName.setText(((MyShipment) c).getClientName());
							
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getAddresseeCodeID()).equals("null")){
									
									tfAddresseeCode.setText("");
									
								}else{
									tfAddresseeCode.setText(((MyShipment) c).getAddresseeCodeID());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the addressee code");
									e.printStackTrace();
								}
							
							tfAddresseeName.setText(((MyShipment) c).getAddresseeName());
							tfInvoice.setText(((MyShipment) c).getInvoice());

							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getrDate()).equals("null")){
									
									tfRDate.setText("");
									
								}else{
									tfRDate.setText(((MyShipment) c).getrDate());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the r.date");
									e.printStackTrace();
								}
							
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getDestinationCodeID()).equals("null")){
									
									tfDestinationCode.setText("");
									
								}else{
									tfDestinationCode.setText(((MyShipment) c).getDestinationCodeID());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the destination code");
									e.printStackTrace();
								}
							
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getdDate()).equals("null")){
									
									tfDDate.setText("");
									
								}else{
									tfDDate.setText(((MyShipment) c).getdDate());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the d.date");
									e.printStackTrace();
								}

							tfCurrency.setText(((MyShipment) c).getComment2());
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getKilos()).equals("null")){
									
									tfKilos.setText("");
									
								}else{
									tfKilos.setText(((MyShipment) c).getKilos());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the Kilos");
									e.printStackTrace();
								}
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getVolume()).equals("null")){
									
									tfVolume.setText("");
									
								}else{
									tfVolume.setText(((MyShipment) c).getVolume());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the volume");
									e.printStackTrace();
								}
							try{
								//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
								if(String.valueOf(((MyShipment) c).getIncotermCodeID()).equals("null")){
									
									tfIncotermCode.setText("");
									
								}else{
									tfIncotermCode.setText(((MyShipment) c).getIncotermCodeID());
								}
							
								}catch(Exception e){
									System.out.println("This is the exception regarding the Incoterm");
									e.printStackTrace();
								}
							
							tfIncotermName.setText(((MyShipment) c).getIncotermName());
							
							
							
							
							tfComment1.setText(((MyShipment) c).getComment1());
							
							
							tfComment2.setText(((MyShipment) c).getComment2());
							
							
							
							System.out.println("intro has been pressed");
							//requestfocus for the next field when pressing enter
							tfClientCode.requestFocus();
							
							}catch(ArrayIndexOutOfBoundsException exception){
								System.out.println("This is the exception release when id not in database");
								
								exception.printStackTrace();
								
								
								tfClientCode.requestFocus();
							}catch(NumberFormatException exception){
								tfCode.setText(null);
								System.out.println("This is the exception release when there is an String");
								exception.printStackTrace();
							}catch(Exception exception){
								System.out.println("General Exception");
								exception.printStackTrace();
							}
						}
						
						
						public void escape(){
							Shipment.getInstance().dispose();
							Menu.getInstance().enableMenu();
						}
						
						public void codeFocus(){
							
							tfCode.requestFocus();
						}
						
						public void checkShipmentBeforeAddItems(){
							
							Glob.setType(SearchType.SHIPMENT);
							
							try {
								if(!tfCode.getText().isEmpty()) {
									if(Integer.parseInt(tfCode.getText())<=0){
										JOptionPane.showMessageDialog(null, "Shipment Code has to be a valid number");
										tfCode.setText("");
										tfCode.requestFocus();
										return;
										
									}
								}
							} catch (NumberFormatException e) {
							
								e.printStackTrace();
								JOptionPane.showMessageDialog(null, "Shipment Code has to be a valid number");
								tfCode.setText("");
								tfCode.requestFocus();
								return;
								
							} catch (HeadlessException e) {
							
								e.printStackTrace();
							}
							
							try {
								
								System.out.println("this is the check id on table happening");
								ScreenHandling.checkIdOnTable(tfCode, SearchType.SHIPMENT);
								
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								return;
							}
							
							if(tfClientCode.getText().isEmpty()) {
								JOptionPane.showMessageDialog(null, "Client code may not be empty");
								tfCode.setText("");
								tfCode.requestFocus();
								return;
							}
							
							ShipmentItems shipmentItems = new ShipmentItems();
							shipmentItems.setNorthModel();
							shipmentItems.setSouthModel();
							shipmentItems.setVisible(true);
							Shipment.getInstance().disableShipment();
							
							System.out.println("this is the end of the method in Shipment when pressed F8");
							
							

						}
						
}





